package com.qiall.gateway.controller;

import com.qiall.assertutil.Assert;
import com.qiall.common.exception.BusinessResult;
import com.qiall.gateway.data.entity.MenuInfo;
import com.qiall.gateway.data.entity.MenuRoleMap;
import com.qiall.gateway.data.mapper.MenuMapper;
import com.qiall.gateway.data.mapper.MenuRoleMapMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menuManage")
public class MenuController {


    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private MenuRoleMapMapper menuRoleMapMapper;

    /**
     * 新增or更新菜单
     */
    @PostMapping("addOrUpdate")
    public BusinessResult<Void> addOrUpdateMenu(@RequestBody MenuInfo menuInfo) {

        int count = menuMapper.countByName(menuInfo.getMenuName());
        if (menuInfo.getId() == null) {
            // 新增
            Assert.isTrue(count == 0, "名称重复");
            menuMapper.insert(menuInfo);
            return BusinessResult.success(null);
        }
        if (count > 0) {
            MenuInfo selectByMenuName = menuMapper.selectByMenuName(menuInfo.getMenuName());
            Assert.isTrue(selectByMenuName.getId().equals(menuInfo.getId()), "名称重复无法更新");
        }
        menuMapper.update(menuInfo);
        return BusinessResult.success(null);
    }

    /**
     * 获取全部菜单
     */
    @GetMapping("getAllMenu")
    public BusinessResult<List<MenuInfo>> getAllMenu() {
        return BusinessResult.success(menuMapper.selectAll());
    }

    /**
     * 角色绑定菜单
     *
     * @param roleMaps
     * @return
     */
    @PostMapping("roleBindMenu")
    public BusinessResult<Void> bindMenu(@RequestBody List<MenuRoleMap> roleMaps) {
        Set<Integer> roleIds = roleMaps.stream().map(x -> x.getRoleId()).collect(Collectors.toSet());
        // 先删除原有绑定关系
         roleIds.forEach(x->{
             menuRoleMapMapper.deleteByRoleId(x);
         });
        for (int i = 0; i < roleMaps.size(); i++) {
            Integer count = menuRoleMapMapper.countByMenuIdAndRoleId(roleMaps.get(i).getRoleId(), roleMaps.get(i).getMenuId());
            if (count > 0) {
                continue;
            }
            menuRoleMapMapper.insert(roleMaps.get(i));
        }
        return BusinessResult.success(null);
    }

    /**
     * 获取角色菜单
     *
     * @return
     */
    @PostMapping("getRoleMenu")
    public BusinessResult<List<MenuInfo>> getRoleMenu(@RequestParam("roleId") Integer roleId) {
        List<MenuRoleMap> menuRoleMaps = menuRoleMapMapper.selectByRoleId(roleId);
        List<MenuInfo> menuInfos = new ArrayList<>();
        for (MenuRoleMap menuRoleMap : menuRoleMaps) {
            MenuInfo menuInfo = menuMapper.selectById(menuRoleMap.getMenuId());
            menuInfos.add(menuInfo);
        }
        return BusinessResult.success(menuInfos);
    }

    /**
     * 删除角色菜单
     *
     * @param menuRoleMap
     * @return
     */
    @PostMapping("delRoleMenu")
    public BusinessResult<Void> delMenuRole(@RequestBody MenuRoleMap menuRoleMap) {
        menuRoleMapMapper.deleteRoleMenu(menuRoleMap.getRoleId(), menuRoleMap.getMenuId());
        return BusinessResult.success(null);
    }

}
